//
//  ResetuserInfoController.m
//  Contacts
//
//  Created by 李 雷川 on 14-8-21.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ResetuserInfoController.h"
#import "Contact.h"
#import "ServerInterface.h"
extern Contact *globalContact;


@interface ResetuserInfoController ()<UITextFieldDelegate,UITextViewDelegate>{
   IBOutlet UITextField   *textField;
 
}

@end

@implementation ResetuserInfoController
@synthesize viewType,resetUserInfoBlock;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(postUserinfo:)];
  
    self.navigationItem.rightBarButtonItem = rightItem;
    switch (viewType) {
        case 0:{
            textField.text = globalContact.mobilephone;
             textField.keyboardType = UIKeyboardTypeNumberPad;
            self.title = @"手机";
        }
            break;
        case 1:{
            textField.keyboardType = UIKeyboardTypeNumberPad;
             textField.text = globalContact.phone;
            self.title = @"座机";
        }
            break;
        case 2:{
            textField.text = globalContact.officeNo;
             self.title = @"办公室";
        }
            
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)postUserinfo:(id)sender{
 
    NSDictionary *userInfoDic;
    switch (viewType) {
        case 0:{
            BOOL  isValid = [self isValidateMobile:textField.text];
            if (isValid) {
                globalContact.mobilephone = textField.text;
                userInfoDic = @{@"user_id":@(globalContact.userID),@"mobile":globalContact.mobilephone,@"company_id":CompanyID,@"token":globalContact.token};
            }
            else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"不是有效的手机号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                return;
            }
        }
            break;
        case 1:{
            globalContact.phone = textField.text;
            userInfoDic = @{@"user_id":@(globalContact.userID),@"phone":globalContact.phone,@"company_id":CompanyID,@"token":globalContact.token};
           
        }
            break;
        case 2:{
             globalContact.officeNo = textField.text;
             userInfoDic = @{@"user_id":@(globalContact.userID),@"office_num": globalContact.officeNo,@"company_id":CompanyID,@"token":globalContact.token};
        }
            break;
        default:
            break;
    }
    [self.navigationController popViewControllerAnimated:YES];
    [ServerInterface updateUserInfoWithInfoDic:userInfoDic withBlock:^(NSDictionary *resultDic, NSError *error) {
        if (error) {

        }
        else{
            if ([[resultDic valueForKeyPath:@"success"] integerValue] ==1) {
                BOOL success =[Contact  updateContact:globalContact];
                if (success) {
                }
            }
            else{
                NSString *message =[resultDic objectForKey:@"message"];
                if (!message) {
                    message = @"更新失败";
                }
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"更新失败" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
        }
    }];
    
    if (resetUserInfoBlock) {
        self.resetUserInfoBlock();
    }
}

-(BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

/*手机号码验证 MODIFIED BY HELENSONG*/
-(BOOL) isValidateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    //    NSLog(@"phoneTest is %@",phoneTest);
    return [phoneTest evaluateWithObject:mobile];
}

/*车牌号验证 MODIFIED BY HELENSONG*/
BOOL validateCarNo(NSString* carNo)
{
    NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    NSLog(@"carTest is %@",carTest);
    return [carTest evaluateWithObject:carNo];
}

#pragma mark ---------判断电话和手机号码是否有效
- (BOOL)isMobileNumber:(NSString *)mobileNum
{

    //判断电话区号后面有－号表达式
    NSString *USUALMOBILE = @"(\\d{2,5}-\\d{7,8}(-\\d{1,})?)|(13\\d{9})|(159\\d{8})";
    //判断是否是有效电话号码表达式
    NSString *PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *Usualregextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", USUALMOBILE];
    NSPredicate *regextesPhs = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
    
    if (([regextesPhs evaluateWithObject:mobileNum] == YES)||([Usualregextestmobile evaluateWithObject:mobileNum] == YES))
    {
        //有效电话或者手机号
        return YES;
    }
    else
    {
        return NO;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
